/**
 * 部署信息管理初始化
 */
var AppDeploy = {
    id: "AppDeployTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    catalogId: 0
};

/**
 * 初始化表格的列
 */
AppDeploy.initColumn = function () {
    return [
        {title: '主键id', visible: false, field: 'id', align: 'center', valign: 'middle', sortable: true},
        {title: '应用id', visible: false, field: 'appId', align: 'center', valign: 'middle', sortable: true},
        {title: '应用编码', field: 'id', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	return row.app.appCode;
        }},
        {title: '应用名称', field: 'id', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	return row.app.appName;
        }},
        {title: '应用类型', field: 'id', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(row.app.appType==1){
                return "<span class='label label-success'>本地应用</span>";
        	}else{
                return "<span class='label label-info'>远程应用</span>";
        	}
        }},
        {title: '应用版本', field: 'appVersion', align: 'center', valign: 'middle', sortable: true},
        {title: '运行状态', field: 'runningState', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value==1){
                return "<span class='label label-success'>运行中</span>";
        	}else{
                return "<span class='label label-danger'>停用</span>";
        	}
        }},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	var _html = "";
        	_html += "<div class='btn-group' role='group' >"
        	_html += "<button type='button' class='btn btn-info btn-xs' onclick='AppDeploy.updateWeight(\""+row.id+"\","+value+",-1)'><i class='fa fa-arrow-circle-down' aria-hidden='true'></i></button>"
        	_html += "<a class='btn btn-default btn-xs disabled'>"+value+"</a>"
        	_html += "<button type='button' class='btn btn-info btn-xs' onclick='AppDeploy.updateWeight(\""+row.id+"\","+value+",1)'><i class='fa fa-arrow-circle-up' aria-hidden='true'></i></button>"
        	_html += "</div>"
        	return _html;
        }},
        {title: '管理', field: 'id',width:'180px', align: 'center', valign: 'middle', sortable: false,formatter:function(value,row,index){
        	var _html = "";
        	if(row.runningState==1){
            	if(row.app.appType==2){
            		if(row.deployType==1){
            			_html += "<button type='button' class='btn btn-info btn-xs btn-margin' onclick='AppDeploy.goApp(\"dev\",\""+row.app.appCode+"\")'><i class='fa fa-paper-plane' aria-hidden='true'></i>  访问</button>"
            		}else if(row.deployType==2){
            			_html += "<button type='button' class='btn btn-info btn-xs btn-margin' onclick='AppDeploy.goApp(\"pro\",\""+row.app.appCode+"\")'><i class='fa fa-paper-plane' aria-hidden='true'></i>  访问</button>"
            		}
            	}
        		_html += "<button type='button' class='btn btn-warning btn-xs btn-margin' onclick='AppDeploy.updateState(\""+row.id+"\",2)'><i class='fa fa-stop' aria-hidden='true'></i>  停用</button>"
        	}else if(row.runningState==2){
        		_html += "<button type='button' class='btn btn-success btn-xs btn-margin' onclick='AppDeploy.updateState(\""+row.id+"\",1)'><i class='fa fa-play' aria-hidden='true'></i>  启用</button>"
        	}
        	_html += "<button type='button' class='btn btn-danger btn-xs btn-margin' onclick='AppDeploy.del(\""+row.id+"\")'><i class='fa fa-trash' aria-hidden='true'></i>  下架</button>"
        	
        	return _html;
        }},
    ];
};
/**
 * 访问
 */
AppDeploy.goApp = function(deployType,appCode){
	window.open(Sys.ctxPath+"/static/app/"+deployType+"/core/login.html");
}
/**
 * 更新状态
 */
AppDeploy.updateState = function(id,state){

    var ajax = new $ax(Sys.backendPath + "/appdeploy/dev/update", function (data) {
        Sys.success("更新成功!");
        AppDeploy.table.refresh();
    }, function (data) {
        Sys.error("更新失败!" + data.responseJSON.message + "!");
    });
    ajax.set("id",id);
	ajax.set("runningState",state);
    ajax.start();
}
/**
 * 更新排序
 */
AppDeploy.updateWeight = function(id,weight,sort){

    var ajax = new $ax(Sys.backendPath + "/appdeploy/dev/update", function (data) {
        Sys.success("更新成功!");
        AppDeploy.table.refresh();
    }, function (data) {
        Sys.error("更新失败!" + data.responseJSON.message + "!");
    });
    ajax.set("id",id);
    var tmp = weight+sort;
    tmp = tmp<1?1:tmp;
	ajax.set("weight",tmp);
    ajax.start();
}

/**
 * 检查是否选中
 */
AppDeploy.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        AppDeploy.seItem = selected[0];
        return true;
    }
};

/**
 * 删除部署信息
 */
AppDeploy.del = function (id) {
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/appdeploy/dev/delete", function (data) {
            Sys.success("下架成功!");
            AppDeploy.table.refresh();
        }, function (data) {
            Sys.error("下架失败!" + data.responseJSON.message + "!");
        });
        ajax.set("appDeployId",id);
        ajax.start();
    };
    Sys.confirm("是否下架应用?",operation);
};

/**
 * 查询部署信息列表
 */
AppDeploy.search = function () {
    var queryData = {};
    queryData['catalogId'] = AppDeploy.catalogId;
    queryData['appName'] = $("#appName").val();
    queryData['appCode'] = $("#appCode").val();
    queryData['appVersion'] = $("#appVersion").val();
    queryData['runningState'] = $("#runningState").val();
    queryData['deployType'] = appDeployType;
    AppDeploy.table.refresh({query: queryData});
};

AppDeploy.onClickAppCatalog = function (e, treeId, treeNode) {
	AppDeploy.catalogId = treeNode.id;
	AppDeploy.search();
};
$(function () {
    var defaultColunms = AppDeploy.initColumn();
    var table = new BSTable(AppDeploy.id, Sys.backendPath + "/appdeploy/dev/list", defaultColunms);
    var queryData = {'deployType':appDeployType};
    table.setQueryParams(queryData);
    table.setPaginationType("server");
    table.setSortName("weight");
    table.setShowRefresh(false);
    table.setShowColumns(false);
    AppDeploy.table = table.init();
    var ztree = new $ZTree("catalogTree", Sys.backendPath + "/appcatalog/tree");
    ztree.bindOnClick(AppDeploy.onClickAppCatalog);
    ztree.init();
});
